/* Based on gate.creole.gazzeteer.FSMState */
package telin;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Implements a state of the deterministic finite state machine of the gazetter.
 * @author Telematica Instituut, Christian Wartena
 */

public class FSMState implements Serializable {

	/**
	 * Constructs a new FSMState object and adds it to the list of states of the
	 * {@link gate.creole.gazetteer.DefaultGazetteer} provided as owner.
	 * 
	 *
	 *            a {@link gate.creole.gazetteer.DefaultGazetteer} object
	 */
	public FSMState() {
		myIndex = index++;
	}

	/**
	 * Adds a new value to the transition function
	 */
	public void put(char chr, FSMState state) {
		transitionFunction.put(chr, state);
	}

	/**
	 * This method is used to access the transition function of this state.
	 */
	public FSMState next(char chr) {// UnicodeType type){
		return (FSMState) transitionFunction.get(chr);
	}

	/**
	 * Returns a GML (Graph Modelling Language) representation of the edges
	 * emerging from this state.
	 */
	public String getEdgesGML()
    {
//		String res = "";
//		char currentChar;
//		FSMState nextState;

//        for (char itemsKey : transitionFunction.itemsKeys) {
//            currentChar = itemsKey;
//            nextState = next(currentChar);
//            res += "edge [ source " + myIndex + " target "
//                    + nextState.getIndex() + " label \"'" + currentChar
//                    + "'\" ]\n";
//        }
		return "";
	}

	/**
	 * Checks whether this state is a final one
	 */
	public boolean isFinal() {
		if (termSet == null)
			return false;
		return !termSet.isEmpty();
	}

	/**
	 * Returns a set of {@link Term} objects describing the types of lookups
	 * the phrase for which this state is the final one belongs to
	 */
	public Set getTermSet() {
		return termSet;
	}

	/**
	 * Adds a new term description to this state's term descriptions set
	 */
	public void addTerm(Term term)
    {
		if (termSet == null)
			termSet = new HashSet<Term>();

		termSet.add(term);
	} 

	/**
	 * Removes a term description from this state's term descriptions set
	 */
	public void removeTerm(Term term) {
		termSet.remove(term);
	} 

	/**
	 * Returns the unique ID of this state.
	 */
	public int getIndex() {
		return myIndex;
	}

	/**
	 * The transition function of this state.
	 */
	protected CharMap transitionFunction = new CharMap();

	protected Set<Term> termSet;

	/**
	 * The unique id of this state. This value is never used by the algorithms
	 * but it can be useful for graphical representations.
	 */
	protected int myIndex;

	/**
	 * Class member used to generate unique ids for the instances
	 * 
	 */
	private static int index;

	static {
		index = 0;
	}

} // class FSMState
